var dateUtils = require('../utils/dateUtil.js');
var dateTimePicker = require('../utils/dateTimePicker.js');

const app = getApp()
import {
  formatDate,formatDateReal
} from '../utils/conmon'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
      
  },

  /**
   * 组件的初始数据
   */
  data: {
      startTime:{
        date: '2018-10-01',
        time: '12:00',
        dateTime: null,
        dateTimeArray: null,
        startYear: 2000,
        endYear: 2150,
        day: '',
       
      },
      firstTitle:'',
      endTime:{
        date: '2018-10-01',
        time: '12:00',
        dateTime: null,
        dateTimeArray: null,
        startYear: 2000,
        endYear: 2150,
        day: '',
      },
      titleArray:[],
      showType:0,
      startTimeStr:'',
      endTimeStr:'',

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onShowAlert(startT,endT,titleArray,type){
// --- 

    var st = new Date();
      
    var defaultDay  =formatDate(new Date());

    var obj = dateTimePicker.dateTimePicker(this.data.startTime.startYear, this.data.startTime.endYear,startT);
  
    var time = dateTimePicker.getHourMinu();
    obj.dateTime[2] = parseInt((obj.defaultDay).substring(0, 2)) - 1; //day 字符串 'xx日' 转 'int'

    
    var startTime ={
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      day: obj.defaultDay,
      time: time
    }
    
    this.setData({
      startTime:startTime,
      showType:type||0
     });
     //console.log("时间 startTime=="+JSON.stringify(this.data.startTime));


    var obj = dateTimePicker.dateTimePicker(this.data.endTime.startYear, this.data.endTime.endYear,endT);
  
    var time = dateTimePicker.getHourMinu();
    obj.dateTime[2] = parseInt((obj.defaultDay).substring(0, 2)) - 1; //day 字符串 'xx日' 转 'int'


    var endTime ={
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      day: obj.defaultDay,
      time: time
    }
    this.setData({
      endTime:endTime,
      titleArray:titleArray||[],
     });

     //console.log("时间 endTime=="+JSON.stringify(this.data.endTime));

     var defaultDay1  =formatDateReal(new Date());
    
     defaultDay1  = defaultDay1.split(' ')[0];


     var starTime = dateUtils.mathChangeDate(dateUtils.formatTime(new Date()),"-",7,true,'');

     this.setData({
      startTimeStr:starTime + ' 00:00:00',
      endTimeStr:defaultDay1 + ' 23:59:59',
     })
     console.log(this.data.startTimeStr+"==时间=="+this.data.endTimeStr)
    },

    onShowSingelAlert(startT,title){


      if(startT){
      
              //
            var defaultDay  =startT;
        
            var obj = dateTimePicker.dateTimePicker(this.data.startTime.startYear, this.data.startTime.endYear,startT);
          
            var time = dateTimePicker.getHourMinu();
            obj.dateTime[2] = parseInt((obj.defaultDay).substring(0, 2)) - 1; //day 字符串 'xx日' 转 'int'
        
            var startTime ={
              dateTime: obj.dateTime,
              dateTimeArray: obj.dateTimeArray,
              day: obj.defaultDay,
              time: time,
              title:title
            }
        
            this.setData({
              startTime:startTime,
              showType:1,
              firstTitle:title
            });
        
            this.setData({
              startTimeStr:startTime,
            
            });
            
            var defaultDay1  =formatDateReal(new Date());

            if(startT){
              defaultDay1  =formatDateReal(new Date(startT));

            }else{

            }
            console.log("11==========="+defaultDay1)
            defaultDay1  = defaultDay1.split(' ')[0]
            console.log("22==========="+defaultDay1)
        
        
        
            this.setData({
              startTimeStr:defaultDay1 + ' 00:00:00',
            })

            }else{
              var st = new Date();
          
            var defaultDay  =formatDate(st);
            console.log(defaultDay)
        
            var obj = dateTimePicker.dateTimePicker(this.data.startTime.startYear, this.data.startTime.endYear);
          
            var time = dateTimePicker.getHourMinu();
            obj.dateTime[2] = parseInt((obj.defaultDay).substring(0, 2)) - 1; //day 字符串 'xx日' 转 'int'
        
            var startTime ={
              dateTime: obj.dateTime,
              dateTimeArray: obj.dateTimeArray,
              day: obj.defaultDay,
              time: time,
              title:title
            }
        
            this.setData({
              startTime:startTime,
              showType:1,
              firstTitle:title
            });
        
            this.setData({
              startTimeStr:startTime,
            
            });
            
            var defaultDay1  =formatDateReal(new Date());

        
            console.log("11==========="+defaultDay1)
            defaultDay1  = defaultDay1.split(' ')[0]
            console.log("22==========="+defaultDay1)
        
        
        
            this.setData({
              startTimeStr:defaultDay1 + ' 00:00:00',
            })
            }
      // --- 
          

     },

    titleClick(envent){//标题回调
      
      var vt = envent.currentTarget.dataset.index;
      this.triggerEvent("titleDateIndex",vt)
    },

    onHideAlert(){

    },

    bindTimeChange: function (e) {
      
      var sendVale = e.detail.value;
      //console.log('picker发送选择改变，携带值为', e.detail.value)

      var arr = this.data.startTime.dateTime, dateArr = this.data.startTime.dateTimeArray;
  
      console.log( e.detail.value)
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      console.log(arr);
      var startTime ={
        dateTimeArray: dateArr,
        dateTime: arr,
        day: dateArr[2][arr[2]].substring(0, 3),
      }

      console.log(JSON.stringify(dateArr))
      this.setData({
        startTimeStr:startTime
      });

      var index0 = sendVale[0];
      var data0 =   dateArr[0][parseInt(index0)]
      
      data0 = parseInt(data0)

      var index1 = sendVale[1];
      var data1 =   dateArr[1][parseInt(index1)]
      data1 = parseInt(data1)

      var index2 = sendVale[2];
      var data2 =   dateArr[2][parseInt(index2)]
      data2 = parseInt(data2)

      if(data1){
        data1 = data1>9?data1:'0'+data1
      }
      if(data2){
        data2 = data2>9?data2:'0'+data2
      }

      var dataStr = data0+'-'+data1+'-'+data2

      console.log("确认时间==="+dataStr)

      
      this.triggerEvent("didClickCallDate",dataStr)
    },


    bindTimeChangeEnd(e){
       
      var sendVale = e.detail.value;
      //console.log('picker发送选择改变，携带值为', e.detail.value)

      var arr = this.data.startTime.dateTime, dateArr = this.data.startTime.dateTimeArray;
  
      console.log( e.detail.value)
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      console.log(arr);
      var startTime ={
        dateTimeArray: dateArr,
        dateTime: arr,
        day: dateArr[2][arr[2]].substring(0, 3),
      }

      console.log(JSON.stringify(dateArr))
      this.setData({
        startTimeStr:startTime
      });

      var index0 = sendVale[0];
      var data0 =   dateArr[0][parseInt(index0)]
      
      data0 = parseInt(data0)

      var index1 = sendVale[1];
      var data1 =   dateArr[1][parseInt(index1)]
      data1 = parseInt(data1)

      var index2 = sendVale[2];
      var data2 =   dateArr[2][parseInt(index2)]
      data2 = parseInt(data2)

      if(data1){
        data1 = data1>9?data1:'0'+data1
      }
      if(data2){
        data2 = data2>9?data2:'0'+data2
      }

      var dataStr = data0+'-'+data1+'-'+data2

      console.log("确认时间End==="+dataStr)

      
      this.triggerEvent("bindTimeChangeEnd",dataStr+' 23:59:59')
     
    },
    bindTimeChangeStart(e){
       
      var sendVale = e.detail.value;
      //console.log('picker发送选择改变，携带值为', e.detail.value)

      var arr = this.data.startTime.dateTime, dateArr = this.data.startTime.dateTimeArray;
  
      console.log( e.detail.value)
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      console.log(arr);
      var startTime ={
        dateTimeArray: dateArr,
        dateTime: arr,
        day: dateArr[2][arr[2]].substring(0, 3),
      }

      console.log(JSON.stringify(dateArr))
      this.setData({
        startTimeStr:startTime
      });

      var index0 = sendVale[0];
      var data0 =   dateArr[0][parseInt(index0)]
      
      data0 = parseInt(data0)

      var index1 = sendVale[1];
      var data1 =   dateArr[1][parseInt(index1)]
      data1 = parseInt(data1)

      var index2 = sendVale[2];
      var data2 =   dateArr[2][parseInt(index2)]
      data2 = parseInt(data2)

      if(data1){
        data1 = data1>9?data1:'0'+data1
      }
      if(data2){
        data2 = data2>9?data2:'0'+data2
      }

      var dataStr = data0+'-'+data1+'-'+data2

      console.log("确认时间Start==="+dataStr)

      
      this.triggerEvent("bindTimeChangeStart",dataStr+' 00:00:00')
    },

    bindDateChange: function (e) {
      console.log(e.detail.value)
      
      this.setData({
        dates: e.detail.value//获取了点击时间的数据
      })
    },

    bindchange:function(e){
      
    },

    changeDateTimeColumnStart(e) {
    
      var arr = this.data.startTime.dateTime, dateArr = this.data.startTime.dateTimeArray;
  
      console.log( e.detail.value)
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      //console.log(arr);
      var startTime ={
        dateTimeArray: dateArr,
        dateTime: arr,
        day: dateArr[2][arr[2]].substring(0, 3),
      }
      this.setData({
        startTime:startTime
      });
    },
    changeDateTimeColumnEnd(e){
      
      var arr = this.data.endTime.dateTime, dateArr = this.data.endTime.dateTimeArray;
  
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      console.log("00====="+dateArr[2][arr[2]]);
      var endTime ={
        dateTimeArray: dateArr,
        dateTime: arr,
        day: dateArr[2][arr[2]].substring(0, 3),
      }
      this.setData({
        endTime:endTime
      });
    }
  

  }
})
